#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;

namespace octalforty.Componento.Shared.SemanticModel
{
    /// <summary>
    /// Represents reference to a component.
    /// </summary>
    [DebuggerDisplay("Name: {Name,nq}, Version: {Version}, Uri: {Uri}")]
    public sealed class ComponentReference : IEquatable<ComponentReference>
    {
        #region Private Fields
        private readonly string name;
        private readonly Version version;
        private readonly Uri uri;
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the name of the component referenced by this <see cref="ComponentReference"/>.
        /// </summary>
        public string Name
        {
            [DebuggerStepThrough]
            get { return name; }
        }

        /// <summary>
        /// Gets a reference to the <see cref="System.Version"/>, which represents
        /// the exact version of the component this <see cref="ComponentReference"/> references.
        /// </summary>
        /// <remarks>
        /// <see cref="Version"/> can be <c>null</c>, in which case <see cref="ComponentReference"/> is
        /// considered to be referencing an unversioned component (for example, a reference to the SVN repo).
        /// </remarks>
        public Version Version
        {
            [DebuggerStepThrough]
            get { return version; }
        }

        /// <summary>
        /// Gets a reference to the <see cref="System.Uri"/>, which represents an URI
        /// the component referenced by this <see cref="ComponentReference"/> can be installed from.
        /// </summary>
        public Uri Uri
        {
            [DebuggerStepThrough]
            get { return uri; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentReference"/> class with the given
        /// <paramref name="name"/> and <paramref name="uri"/>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="uri"></param>
        public ComponentReference(string name, Uri uri)
        {
            this.name = name;
            this.uri = uri;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentReference"/> class with the given
        /// <paramref name="name"/>, <paramref name="version"/> and <paramref name="uri"/>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="uri"></param>
        public ComponentReference(string name, Version version, Uri uri) :
            this(name, uri)
        {
            this.name = name;
            this.version = version;
            this.uri = uri;
        }

        #region IEquatable<ComponentReference> Members
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the other parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ComponentReference other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return Equals(other.name, name) && Equals(other.version, version) && Equals(other.uri, uri);
        }
        #endregion

        #region Object Members
        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof(ComponentReference) && Equals((ComponentReference)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (name != null ? name.GetHashCode() : 0);
                result = (result * 397) ^ (version != null ? version.GetHashCode() : 0);
                result = (result * 397) ^ (uri != null ? uri.GetHashCode() : 0);
                return result;
            }
        }
        #endregion
    }
}